package com.seng.deptservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seng.deptservice.entity.TbDepartment;
import com.seng.deptservice.service.TbDepartmentService;
import com.seng.utilscommon.pwdmd5.MD5Utils;
import com.seng.utilscommon.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author seng
 * @since 2020-07-26
 */
@RestController
@RequestMapping("/deptservice/tb-department")
@CrossOrigin
public class TbDepartmentController {

    @Resource
    private TbDepartmentService tbDepartmentService;

    @ApiOperation(value = "查询所有部门信息")
    @GetMapping("/findDeptList")
    public Result findDeptList(){
        QueryWrapper<TbDepartment> queryWrapper=new QueryWrapper<>();
        //queryWrapper.select("name");
        List<TbDepartment> list = tbDepartmentService.list(queryWrapper);
       return Result.ok().data("list",list);
    }

    @ApiOperation(value = "查询所有部门信息")
    @GetMapping("/findDeptAndCount")
    public Result findDeptAndCount(){
        List<TbDepartment> deptAndCount = tbDepartmentService.findDeptAndCount();
        return Result.ok().data("deptAndCount",deptAndCount);
    }


    @ApiOperation(value = "分组查询部门主任")
    @GetMapping("/findUsername")
    public Result findUsername() {
        List<TbDepartment> deptUsername = tbDepartmentService.findUsername();
        return Result.ok().data("deptUsername", deptUsername);
    }


    @ApiOperation(value = "查询所有部门信息/部门首页中的查询")
    @PostMapping("/findAll/{current}/{size}")
    public Result findAll(@PathVariable("current") Integer current,
                          @PathVariable("size") Integer size,
                          @RequestBody(required = false) TbDepartment tbDepartment){
        Page<TbDepartment> page = new Page<>(current,size);
        QueryWrapper<TbDepartment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("d.deleted",0);
        if (!StringUtils.isEmpty(tbDepartment.getName())){
            queryWrapper.like("name",tbDepartment.getName());
        }
        IPage<TbDepartment> studentPage = tbDepartmentService.findStudentPage(page, queryWrapper);
        List<TbDepartment> findUsername = tbDepartmentService.findUsername();
        return Result.ok().data("depts",studentPage).data("deptUsername",findUsername);
    }

    @ApiOperation(value = "逻辑删除部门信息")
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        System.out.println(id);
        boolean delete = tbDepartmentService.removeById(id);
        if (delete) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @ApiOperation(value = "新增用户信息")
    @PostMapping(value = "/addDept")
    public Result addUser(@RequestBody TbDepartment tbDepartment) {
        tbDepartment.setCreateTime(new Date());

        boolean user = tbDepartmentService.save(tbDepartment);
        if (user) {
            return Result.ok().data("add", "ok");
        } else {
            return Result.error();
        }
    }


    @ApiOperation(value = "根据id进行修改")
    @PutMapping("/updateById")
    public Result updateById(@RequestBody TbDepartment tbDepartment) {
        System.out.println(tbDepartment.toString());
        tbDepartment.setModifiedTime(new Date());
        boolean updatebyId = tbDepartmentService.updateById(tbDepartment);
        return Result.ok().data("updatebyId", updatebyId);
    }

    @ApiOperation(value = "导出部门信息")
    @GetMapping(value = "/export")
    public void exportStudentInfoExcel(HttpServletResponse response) throws IOException {
        tbDepartmentService.exportStudentInfoExcel(response);
    }
}

